package cn.edu.abtu.service.impl;

import cn.edu.abtu.client.VodClient;
import cn.edu.abtu.entity.EduVideo;
import cn.edu.abtu.dao.EduVideoDao;
import cn.edu.abtu.service.EduVideoService;
import cn.edu.abtu.vo.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class EduVideoServiceImpl implements EduVideoService {

    @Autowired
    private EduVideoDao eduVideoDao;

    @Autowired
    private VodClient vodClient;


    @Override
    public ResponseData addVideo(EduVideo eduVideo) {
        eduVideo.setGmtCreate(new Date());
        eduVideo.setGmtModified(new Date());
        int insert = eduVideoDao.insert(eduVideo);
        if (insert>0){
            return ResponseData.success("添加小节信息成功",null);
        }else {
            return ResponseData.fail("添加小节信息失败",null);
        }
    }

    @Override
    public ResponseData deleteVideo(String videoId) {
        EduVideo eduVideo = eduVideoDao.selectById(videoId);
        //查询视频ID
        String videoSourceId = eduVideo.getVideoSourceId();
        System.out.println(videoSourceId);
        if (!StringUtils.isEmpty(videoSourceId)){
            //调用service-vod服务中的通过视频ID删除视频的方法
            ResponseData responseData = vodClient.removeVideo(videoSourceId);
        }
        int i = eduVideoDao.deleteById(videoId);
        if (i>0){
            return ResponseData.success(eduVideo.getTitle()+"删除成功",null);
        }else {
            return ResponseData.fail("删除失败",null);
        }
    }

    @Override
    public ResponseData updateVideo(EduVideo eduVideo) {
        int update = eduVideoDao.updateById(eduVideo);
        if (update>0){
            return ResponseData.success("修改成功",null);
        }else {
            return ResponseData.fail("修改失败",null);
        }
    }
}
